Checkbox
Checkboxes allow users to select one or more items from a list.
#Examples
#Base checkbox
This interactive input control indicates the current state. By default, it is unchecked.
const [checked, setChecked] = useState(false);
return <BaseCheckbox checked={checked} onChange={(e) => setChecked(e.target.checked)} />;
#Checkbox
Provide a label indicating to users what can be selected or deselected.
const [checked, setChecked] = useState(false);
return (
<Checkbox value="value" checked={checked} onChange={(e) => setChecked(e.target.checked)}>
Label
</Checkbox>
);
#Checkbox group
When using CheckboxGroup
, provide a group label to indicate the category of the options and explain what can be selected.
Default state:
by default, checkboxes are unchecked, meaning no options have been selected. Use checked state when the item is selected.Indeterminate state:
use the indeterminate state when you have a list of selections, where some items are are selected, and some are unselected.Disabled state:
when the user expects a predefined set of options, a disabled option is useful to indicate that normally that option would be available, but now it is not available due to a specific condition. If possible, provide hint text or a visual cue to checkbox is disabled to avoid confusion for the user.Label with dynamic text:
be aware of how the labels will resize and how any visuals surrounding them will be affected.Label with icon or tooltip:
help the user understand the context of the checkbox.
const [selected, setSelected] = useState<string[]>([]);
return (
<CheckboxGroup onChange={setSelected} value={selected}>
<CheckboxGroup.Checkbox value="option-1">Regular option</CheckboxGroup.Checkbox>
<CheckboxGroup.Checkbox value="option-2" indeterminate>
Indeterminate option
</CheckboxGroup.Checkbox>
<CheckboxGroup.Checkbox value="option-3" disabled>
Disabled option
</CheckboxGroup.Checkbox>
<CheckboxGroup.Checkbox value="option-4">
<Icon>
<IconApprove />
</Icon>
<InlineText>Option with left icon</InlineText>
</CheckboxGroup.Checkbox>
<CheckboxGroup.Checkbox value="option-5">
{(props: { checked: boolean; disabled: boolean }) => (
<>
<InlineText
emphasis={props.checked ? "medium" : "normal"}
lineHeight="multi-line"
tone={props.disabled ? "subtle" : undefined}
>
Option with right icon and dynamic text style
</InlineText>
<Icon>
<IconApprove />
</Icon>
</>
)}
</CheckboxGroup.Checkbox>
<Tooltip variant={{ type: "interactive" }} content="Extra info on the option 5">
<CheckboxGroup.Checkbox value="option-6">Option with tooltip</CheckboxGroup.Checkbox>
</Tooltip>
</CheckboxGroup>
);
#Horizontal checkbox group
Make a horizontal group by using the direction
prop on the CheckboxGroup
component.
const [selected, setSelected] = useState<string[]>([]);
return (
<CheckboxGroup onChange={setSelected} value={selected} direction="horizontal">
<CheckboxGroup.Checkbox value="option-1">Regular option</CheckboxGroup.Checkbox>
<CheckboxGroup.Checkbox value="option-2" indeterminate>
Indeterminate option
</CheckboxGroup.Checkbox>
<CheckboxGroup.Checkbox value="option-3" disabled>
Disabled option
</CheckboxGroup.Checkbox>
<CheckboxGroup.Checkbox value="option-4">
<Icon>
<IconApprove />
</Icon>
<InlineText>Option with left icon</InlineText>
</CheckboxGroup.Checkbox>
<Tooltip variant={{ type: "interactive" }} content="Extra info on the option 5">
<CheckboxGroup.Checkbox value="option-6">Option with tooltip</CheckboxGroup.Checkbox>
</Tooltip>
</CheckboxGroup>
);
#Properties
#Base checkbox
Property | Description | Defined | Value |
---|---|---|---|
checkedRequired | boolean Determines if the checkbox appears checked or not | ||
onChangeRequired | function Event for when the state of the checkbox changes | ||
idOptional | string Id applied to the form control | ||
onBlurOptional | function Callback for onBlur event | ||
aria-describedbyOptional | string ID of an an element that describes what the form control is for | ||
aria-labelOptional | string Label of the form control | ||
aria-labelledbyOptional | string ID of an an element that labels this form control | ||
valueOptional | string Value of the form control | ||
nameOptional | string Name applied to the form control | ||
invalidOptional | boolean Is the form control invalid | ||
disabledOptional | boolean Disables the checkbox, prevent it from changing state | ||
indeterminateOptional | boolean Shows the checkbox as neither checked nor unchecked | ||
data-observe-keyOptional | string Unique string, used by external script e.g. for event tracking | ||
classNameOptional | string Custom className that's applied to the outermost element (only intended for special cases) | ||
styleOptional | object Style object to apply custom inline styles (only intended for special cases) | ||
tabIndexOptional | number Tab index of the outermost HTML element of the component | ||
onKeyDownOptional | function Callback for onKeyDown event | ||
onMouseDownOptional | function Callback for onMouseDown event | ||
onMouseEnterOptional | function Callback for onMouseEnter event | ||
onMouseLeaveOptional | function Callback for onMouseLeave event | ||
onFocusOptional | function Callback for onFocus event |
#Checkbox
Property | Description | Defined | Value |
---|---|---|---|
onChangeRequired | function Event for when the state of the checkbox changes | ||
checkedRequired | boolean Determines if the checkbox appears checked or not | ||
valueRequired | string Value of the form control | ||
idOptional | string Id applied to the form control | ||
disabledOptional | boolean Disables the checkbox, prevent it from changing state | ||
classNameOptional | string Custom className that's applied to the outermost element (only intended for special cases) | ||
onKeyDownOptional | function Callback for onKeyDown event | ||
onBlurOptional | function Callback for onBlur event | ||
onMouseDownOptional | function Callback for onMouseDown event | ||
aria-describedbyOptional | string ID of an an element that describes what the form control is for | ||
aria-labelOptional | string Label of the form control | ||
aria-labelledbyOptional | string ID of an an element that labels this form control | ||
data-observe-keyOptional | string Unique string, used by external script e.g. for event tracking | ||
styleOptional | object Style object to apply custom inline styles (only intended for special cases) | ||
tabIndexOptional | number Tab index of the outermost HTML element of the component | ||
onMouseEnterOptional | function Callback for onMouseEnter event | ||
onMouseLeaveOptional | function Callback for onMouseLeave event | ||
onFocusOptional | function Callback for onFocus event | ||
nameOptional | string Name applied to the form control | ||
invalidOptional | boolean Is the form control invalid | ||
indeterminateOptional | boolean Shows the checkbox as neither checked nor unchecked | ||
childrenOptional | element |
#Checkbox group
Property | Description | Defined | Value |
---|---|---|---|
valueRequired | string[] Value of the form control | ||
onChangeRequired | function Callback for onChange event | ||
nameOptional | string Name applied to the form control | ||
idOptional | string Id applied to the form control | ||
invalidOptional | boolean Is the form control invalid | ||
onBlurOptional | function Callback for onBlur event | ||
aria-labelOptional | string Label of the form control | ||
aria-describedbyOptional | string ID of an an element that describes what the form control is for | ||
aria-labelledbyOptional | string ID of an an element that labels this form control | ||
childrenOptional | element | ||
directionOptional | "horizontal" | "vertical" | ||
classNameOptional | string Custom className that's applied to the outermost element (only intended for special cases) | ||
styleOptional | object Style object to apply custom inline styles (only intended for special cases) |
#Guidelines
#Best practices
#Do not use when
#Accessibility
Explore detailed guidelines for this component: Accessibility Specifications
#Writing
#Notable changes
#Migrating from FancyLab 16.x to FancyLab 17.x
The CheckboxGroup
component on FancyLab 16.x or prior takes an array of control
objects as input/property, which is terse and works well with typechecking, but it plays poorly with composition. As an example, adding a tooltip to a checkbox label is not possible using this structure as well as changing the icon position to after or before the label.
So, in FancyLab 17.x, the CheckboxGroup
component was refactored to allow composing other components with them. Now, instead of having a controls object, subcomponents are used to create the controls.
Please find below an example that illustrates the usage before and after these changes. Note that the controls
property has been replaced by children of type CheckboxGroup.Checkbox
.
#Before
<CheckboxGroup
onChange={setSelected}
value={selected}
controls={[
{ label: "White Wine", value: "white-wine" },
{ label: "Red Wine", value: "red-wine" },
]}
/>
#After
<CheckboxGroup onChange={setSelected} value={selected}>
<CheckboxGroup.Checkbox value="white-wine">White Wine</CheckboxGroup.Checkbox>
<CheckboxGroup.Checkbox value="red-wine">Red Wine</CheckboxGroup.Checkbox>
</CheckboxGroup>